package com.library;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.Event;
import javax.portlet.EventRequest;
import javax.portlet.EventResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;

import com.library.slayer.model.Book;
import com.library.slayer.service.BookLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.security.permission.PermissionChecker;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class LibraryPortlet
 */
public class LibraryPortlet extends MVCPortlet {
 
	public void addBook(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
		
		// permission check here....
		
		String bookTitle = ParamUtil.getString(actionRequest, "bookTitle");
		String author = ParamUtil.getString(actionRequest, "author");

		try {
			Book book = BookLocalServiceUtil.create(bookTitle, author);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// code to avoid refresh problem
		String redirectURL = ParamUtil.getString(actionRequest, "redirectURL");
		actionResponse.sendRedirect(redirectURL);
	}
	
	public void deleteBook(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
		
		long bookId = ParamUtil.getLong(actionRequest, "bookId");
		
		try {
			BookLocalServiceUtil.deleteBook(bookId);
		} catch (PortalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// code to avoid refresh problem
		String redirectURL = ParamUtil.getString(actionRequest, "redirectURL");
		actionResponse.sendRedirect(redirectURL);
	}
	
	public void updateBook(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
		
		long bookId = ParamUtil.getLong(actionRequest, "bookId");
		
		if (bookId > 0l) {
			
			String bookTitle = ParamUtil.getString(actionRequest, "bookTitle");
			String author = ParamUtil.getString(actionRequest, "author");

			try {
				Book book = BookLocalServiceUtil.getBook(bookId);
				
				book.setBookTitle(bookTitle);
				book.setAuthor(author);
				book.setDateModified(new Date());

				BookLocalServiceUtil.updateBook(book);
			} catch (PortalException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void quickFind(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException, SystemException {
		
		String bookTitle = ParamUtil.getString(actionRequest, "bookTitle");
	
		List<Book> books = null;
		
		String searchType = ParamUtil.getString(actionRequest, "searchType");
		
		if (searchType.equals("search")) {
			books = BookLocalServiceUtil.search(bookTitle);
		} else {
			books = BookLocalServiceUtil.quickFind(bookTitle);
		}
		
		actionRequest.setAttribute("BOOKS", books);
		
		actionResponse.setRenderParameter("jspPage", "/html/library/list.jsp");
	}
	
	@javax.portlet.ProcessEvent(qname = "{http://sun.com}Book")
	public void quickAdd(EventRequest request, EventResponse response)
			throws PortletException, IOException {
		// TODO Auto-generated method stub
		super.processEvent(request, response);
		System.out.println("inside process");
		Event event = request.getEvent();
		
		Book book = (Book)event.getValue();
		book.setDateAdded(new Date());
		try {
			BookLocalServiceUtil.addBook(book);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		response.setRenderParameter("jspPage", "/html/library/list.jsp");
	}
	
	public void saveSettings(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
		
		String greeting = ParamUtil.getString(actionRequest, "greeting");
		
		PortletPreferences prefs = actionRequest.getPreferences();
		
		prefs.setValue("greeting-message", greeting);
		prefs.store();
		
	}
}